package cc.android.testapp.xp.hooker.textmodify.et;

import android.content.Context;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import cc.android.testapp.util.CLog;
import cc.android.testapp.xp.hooker.textmodify.HookEditText;
import cc.commons.util.reflect.ClassUtil;
import cc.commons.util.reflect.FieldUtil;
import cc.commons.util.reflect.MethodUtil;

public class ET_CustomTextView implements IET {
    public boolean INIT_SUCCESS = false;
    public CTView mCTView = null;
    public Class<?> mClazz = null;
    public Method mMethod_getText = null;
    public Method mMethod_setText = null;

    protected View mOrigin = null;

    public ET_CustomTextView() {
    }

    public boolean init(Context pContext, CTView pCTView) {
        this.INIT_SUCCESS = false;
        ClassLoader pLoader = pContext.getClassLoader();
        try {
            if (ClassUtil.isClassLoaded(pCTView.clazz, pLoader)) {
                this.mCTView = pCTView;
                this.mClazz = ClassUtil.getClass(pCTView.clazz, pLoader);
                int tResult = 0;
                for (Method sMethod : mClazz.getDeclaredMethods()) {
                    String tMStr = sMethod.toString();
                    if (tMStr.contains(pCTView.getMethod)) {
                        this.mMethod_getText = sMethod;
                        tResult++;
                    } else if (tMStr.contains(pCTView.setMethod)) {
                        this.mMethod_setText = sMethod;
                        tResult++;
                    }
                    if (tResult >= 2) break;
                }

                if (tResult >= 2) this.INIT_SUCCESS = true;
            }
        } catch (Throwable e) {
            CLog.log("在初始化WX文本框时发生错误", e);
        }
        return this.INIT_SUCCESS;
    }

    @Override
    public CharSequence getText() {
        return (CharSequence) MethodUtil.invokeMethod(mMethod_getText, this.mOrigin);
    }

    @Override
    public void setText(CharSequence pStr) {
        MethodUtil.invokeMethod(mMethod_setText, this.mOrigin, pStr);
    }

    @Override
    public View getOrigin() {
        return this.mOrigin;
    }

    @Override
    public void setOrigin(View pOrigin) {
        this.mOrigin = pOrigin;

        if (INIT_SUCCESS && !pOrigin.getClass().getClassLoader().equals(mClazz.getClassLoader())) {
            init(pOrigin.getContext(), this.mCTView);
        }
    }

    @Override
    public IET copy() {
        ET_CustomTextView t = new ET_CustomTextView();
        for (Field sField : ET_CustomTextView.class.getDeclaredFields()) {
            FieldUtil.setFieldValue(sField, t, FieldUtil.getFieldValue(sField, this));
        }
        return t;
    }

    public static class CTView {
        public final String clazz;
        public final String getMethod;
        public final String setMethod;

        public CTView(String pClazz, String pGetMethod, String pSetMethod) {
            clazz = pClazz;
            getMethod = pGetMethod;
            setMethod = pSetMethod;
        }

    }
}
